<?php
/**
 * Created by PhpStorm.
 * User: wangzhen
 * Date: 2022/2/22
 * Time: 4:05 PM
 */

 namespace App\Admin\Field;

use App\Admin\Repositories\Family;
use App\Exceptions\Arr;
use App\Models\Dictionary;
use App\Models\FamilyType;
use App\Models\HospitalAppointInterval;
use Dcat\Admin\Form\Field;

class HospitalAppointMonthField extends Field
{
    protected $view = 'admin.hospital_appoint_month_field';

    protected static $month = [
        1=>[
            ['month'=>1,'month_name'=>'满月','is_check'=>0],
        ],
        2=>[
            ['month'=>2,'month_name'=>'2个月','is_check'=>0],
            ['month'=>3,'month_name'=>'3个月','is_check'=>0],
        ],
        3=>[
            ['month'=>5,'month_name'=>'5个月','is_check'=>0],
            ['month'=>6,'month_name'=>'6个月','is_check'=>0],

        ],
        4=>[
            ['month'=>8,'month_name'=>'8个月','is_check'=>0],
            ['month'=>9,'month_name'=>'9个月','is_check'=>0],

        ],
        5=>[
            ['month'=>11,'month_name'=>'11个月','is_check'=>0],
            ['month'=>12,'month_name'=>'12个月','is_check'=>0],

        ],
        6=>[
            ['month'=>17,'month_name'=>'1岁5个月','is_check'=>0],
            ['month'=>18,'month_name'=>'1岁6个月','is_check'=>0],
        ],
        7=>[
            ['month'=>23,'month_name'=>'1岁11个月','is_check'=>0],
            ['month'=>24,'month_name'=>'1岁11个月','is_check'=>0],
        ],
        8=>[
            ['month'=>29,'month_name'=>'2岁5个月','is_check'=>0],
            ['month'=>30,'month_name'=>'2岁6个月','is_check'=>0],
        ],
        9=>[
            ['month'=>35,'month_name'=>'2岁11个月','is_check'=>0],
            ['month'=>36,'month_name'=>'2岁12个月','is_check'=>0],
            ['month'=>37,'month_name'=>'3岁1个月','is_check'=>0],
            ['month'=>38,'month_name'=>'3岁2个月','is_check'=>0],
            ['month'=>39,'month_name'=>'3岁3个月','is_check'=>0],
            ['month'=>40,'month_name'=>'3岁4个月','is_check'=>0],
            ['month'=>41,'month_name'=>'3岁5个月','is_check'=>0],
            ['month'=>42,'month_name'=>'3岁6个月','is_check'=>0],
            ['month'=>43,'month_name'=>'3岁7个月','is_check'=>0],
            ['month'=>44,'month_name'=>'3岁8个月','is_check'=>0],
            ['month'=>45,'month_name'=>'3岁9个月','is_check'=>0],
            ['month'=>46,'month_name'=>'3岁10个月','is_check'=>0],
            ['month'=>47,'month_name'=>'3岁11个月','is_check'=>0],
            ['month'=>48,'month_name'=>'3岁12个月','is_check'=>0],
        ],
    ];

    public function __construct($column, $arguments = [])
    {
        parent::__construct($column, $arguments);
        $this->value = value( $this->setData());

    }

    public function setData()
    {
        $child_types = FamilyType::query()->where('examination',1)->get();
        $rs = [];
        foreach($child_types as $k=>$v){
            if(isset(self::$month[$v->id])){
                $row['type_name'] = $v->name;
                $row['type_id'] = $v->id;
                $row['is_open'] = 0;
                $row['months'] = self::$month[$v->id];
                $rs[]=$row;
            }
        }
        return $rs;
    }

    // 把用户输入的表单值转化为 string 格式保存到数据库
    protected function prepareInputValue($array)
    {
        $data = $this->setData();
        foreach($data as $k=>&$v){
            if(in_array($v['type_id'],$array['open'])){
                $v['is_open'] = 1;
            }
            foreach($v['months'] as $vk=>&$vv){
                if(isset($array['month'][$v['type_id']]) && in_array($vv['month'],$array['month'][$v['type_id']])){
                    $vv['is_check'] = 1;
                }
            }
        }
        return json_encode($data);
    }
    protected function formatFieldData($data)
    {
        if(isset($data['month'])){
            return json_decode($data['month'],true);
        }
        return parent::formatFieldData($data);
    }
}
